package com.genesis.yunnanji.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.activity.House;
import com.genesis.yunnanji.activity.HouseDetails;
import com.genesis.yunnanji.adapter.HouseListAdapter;
import com.genesis.yunnanji.adapter.SpecialtyAdapter;
import com.genesis.yunnanji.bean.HouseBean;
import com.genesis.yunnanji.bean.SpecialtyBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONObject;
import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/22.
 */

public class Search_House extends Fragment {

    private View view;
    private Toolbar mToolbar;
    private RecyclerView mRecyclerView;
    private TwinklingRefreshLayout refreshLayout;
    private int page = 1;
    private List<HouseBean.ResultBean.ListBean> list;
    private HouseListAdapter adapter;
    private String wd = "";
    private View nodata;

    public static Search_House newInstance(String param) {
        Search_House obj = new Search_House();
        Bundle bundle = new Bundle();
        bundle.putString("args1", param);
        obj.setArguments(bundle);
        return obj;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = this.getActivity().getLayoutInflater().inflate(R.layout.activity_house, null);
            mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_house_list);
            refreshLayout = (TwinklingRefreshLayout) view.findViewById(R.id.bga_house_refresh);
            mToolbar = (Toolbar) view.findViewById(R.id.tb_house_toolbar);
            mToolbar.setVisibility(View.GONE);
            mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getContext()).size((int) getResources().getDimension(R.dimen.x20)).color(Color.parseColor("#f5f5f5")).build());
            nodata = view.findViewById(R.id.layout_nodata);
            initView();
            getData(null);
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        return view;

    }

    public void setWd(String key) {
        wd = key;
        page = 1;
        if (list != null) {
            list.removeAll(list);
        }
        getData(null);
    }

    protected void initView() {
        list = new ArrayList<>();
        LoadingView loadingView = new LoadingView(this.getActivity());
        refreshLayout.setBottomView(loadingView);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                page = 1;
                list.removeAll(list);
                getData(refreshLayout);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                page += 1;
                getData(refreshLayout);
            }
        });
    }

    private void getData(final TwinklingRefreshLayout refreshLayout) {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.SEARCH);
        params.addBodyParameter("soucre", "house");
        params.addBodyParameter("p", page + "");
        params.addBodyParameter("wd", wd);
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();


                HouseBean houseBean = gson.fromJson(jsonObject.toString(), HouseBean.class);
                if (houseBean.getStatus() == 200) {
                    nodata.setVisibility(View.GONE);
                    for (int j = 0; j < houseBean.getResult().getList().size(); j++) {
                        list.add(houseBean.getResult().getList().get(j));
                    }
                    if (page == 1) {
                        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                        mRecyclerView.setNestedScrollingEnabled(false);
                        adapter = new HouseListAdapter(getContext(), list);
                        mRecyclerView.setAdapter(adapter);
                        LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.x15));
                        LinearLayout lineLayout = new LinearLayout(getContext());
                        lineLayout.setOrientation(LinearLayout.VERTICAL);
                        lineLayout.setLayoutParams(params);
                        lineLayout.setBackgroundColor(getResources().getColor(R.color.divider));
                        adapter.addHeaderView(lineLayout);
                        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                Intent intent = new Intent(getContext(), HouseDetails.class);
                                intent.putExtra("houseid", list.get(position).getId());
                                startActivity(intent);
                            }
                        });
                    } else {
                        adapter.notifyDataSetChanged();
                    }

                } else {
                    if (page == 1) {
                        nodata.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {
                if (refreshLayout != null) {
                    refreshLayout.finishRefreshing();
                    refreshLayout.finishLoadmore();
                }
            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }

}
